﻿#include "SoftwareRegisterDialog.h"

#include <QDebug>
#include <QMessageBox>

namespace Core {

	SoftwareRegisterDialog::SoftwareRegisterDialog(QWidget *parent, Qt::WindowFlags f)
		: QDialog(parent, f)
		, m_pRegistrationAlgorithm(nullptr)
	{
		ui.setupUi(this);
		this->setWindowTitle(u8"软件注册");
		this->initUI();
	}

	SoftwareRegisterDialog::~SoftwareRegisterDialog()
	{
		if (m_pRegistrationAlgorithm != nullptr)
		{
			delete m_pRegistrationAlgorithm;
			m_pRegistrationAlgorithm = nullptr;
		}
	}

	void SoftwareRegisterDialog::slotGetMachineCodeBtnClicked()
	{
		QString strMachineCode = m_pRegistrationAlgorithm->getMachineCode();
		if (strMachineCode.isEmpty())
		{
			return;
		}
		ui.lineEdit_machineCode->setText(strMachineCode);
	}

	void SoftwareRegisterDialog::slotWriteRegistrationCodeBtnClicked()
	{
		if (ui.textEdit_registrationCode->toPlainText().isEmpty())
		{
			QMessageBox::warning(nullptr, u8"提示", u8"输入的软件注册码为空，请检查信息");
			return;
		}

		QString strRegistrationCode = ui.textEdit_registrationCode->toPlainText();

		bool bState = m_pRegistrationAlgorithm->writeRegistrationCode(strRegistrationCode);
		QMessageBox::warning(nullptr, u8"提示", bState ? u8"软件注册成功" : u8"软件注册失败");
	}

	void SoftwareRegisterDialog::slotTestRegistrationBtnClicked()
	{
		bool bState = m_pRegistrationAlgorithm->verifyRegistration();
		QMessageBox::warning(nullptr, u8"提示", bState ? u8"软件已注册" : u8"软件未注册/注册已过期");
	}

	void SoftwareRegisterDialog::initUI()
	{
		m_pRegistrationAlgorithm = new RegistrationAlgorithm(this);

		connect(ui.pushButton_machineCode, &QPushButton::clicked, this, &SoftwareRegisterDialog::slotGetMachineCodeBtnClicked);

		connect(ui.pushButton_register, &QPushButton::clicked, this, &SoftwareRegisterDialog::slotWriteRegistrationCodeBtnClicked);

		connect(ui.pushButton_test, &QPushButton::clicked, this, &SoftwareRegisterDialog::slotTestRegistrationBtnClicked);
	}

}//namespace Core 